<template>
	<view class="body">
		<view class="title"  v-if="switchInfo.newbm == '1' && !updateFlag">
			当前尚未开放注册新学员，
			<br />请联系学校电话：0743-8226214
		</view>
		<u--form labelPosition="left" :model="model" label-width="80" ref="uForm">
			<u-form-item label="姓名" prop="userInfo.tname" borderBottom ref="item1">
				<u--input v-model="model.userInfo.tname" border="none" placeholder="请填写真实姓名"></u--input>
			</u-form-item>
			<u-form-item label="性别" prop="userInfo.sex" borderBottom @click="showSex = true" ref="item1">
				<u--input v-model="model.userInfo.sex" readonly placeholder="请选择性别" border="none"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			
			<u-form-item label="单位" prop="userInfo.cp" borderBottom ref="item1">
				<u--input v-model="model.userInfo.cp" border="none" placeholder="请填写所在单位"></u--input>
			</u-form-item>
			<u-form-item label="身份证号" prop="userInfo.sfz" borderBottom ref="item1">
				<u--input v-model="model.userInfo.sfz" border="none" placeholder="请填写真实的身份证号码"></u--input>
			</u-form-item>
			<u-form-item label="政治面貌(是否党员)" prop="userInfo.dy" borderBottom ref="item1" @click="showPolitics = true">
				<u--input v-model="model.userInfo.dy" readonly placeholder="请选择政治面貌" border="none"></u--input>
				<u-picker :show="showPolitics" :columns="politicsStatus" title="是否是党员" keyName="name"
					@cancel="showPolitics = false" @confirm="politics"></u-picker>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			<u-form-item label="本人电话" prop="userInfo.tel" borderBottom ref="item1">
				<u--input v-model="model.userInfo.tel" border="none" placeholder="请填写本人电话"></u--input>
			</u-form-item>
			<u-form-item label="家属电话" prop="userInfo.phone" borderBottom ref="item1">
				<u--input v-model="model.userInfo.phone" border="none" placeholder="请填写家属电话"></u--input>
			</u-form-item>
		</u--form>
		<u-button @click="submit" type="primary" style="margin: 10rpx 0">提交</u-button>
		<u-button @click="reset" type="warning" style="margin: 10rpx 0">重置</u-button>
		<u-action-sheet :show="showSex" :actions="actions" title="请选择性别" @close="showSex = false" @select="sexSelect">
		</u-action-sheet>
		<u-modal :show="show" @confirm="dialogConfirm">
			<view class="slot-content scrollable-container">
				<iframe v-if="show" src="../../static/xieyi/notice.html" width="100%" height="500px" frameborder="0"></iframe>
			</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				showSex: false,
				showTime: false,
				showPolitics: false,
				time: Number(new Date()),
				fileList1: [],
				model: {
					userInfo: {
						tname: "",
						sex: "",
						dy: '',
						sfz:'',
						cp: '',
						tel: '',
						phone: '',
					},
				},
				actions: [{
						name: "男",
					},
					{
						name: "女",
					},
				],
				politicsStatus: [
					[{
							name: "是",
						},
						{
							name: "否",
						},
					],
				],
				radio: "",
				switchVal: false,
				id:0,
				xh:null,
				switchInfo:{
					newbm:'',
				},
				updateFlag:false,
			};
		},
		onLoad(e) {
			this.getSwitch()
			console.log(e,'e');
			let a = uni.getStorageSync('dialogIfShowb')
			a = a === false ? false : true
			if(a){
				this.show = true
			}
			this.updateFlag = !!e.val
			if(!!e.val){
				let info = JSON.parse(e.val)
				this.id = info.id
				this.xh = info.xh
				console.log(info,'info');
				this.model.userInfo = ((obj)=> {
					return {
						tname: obj.tname,
						sex: obj.sex,
						dy: obj.dy,
						sfz: obj.sfz,
						cp: obj.cp,
						tel: obj.tel,
						phone: obj.phone
					}
				})(info)
			}
		},
		methods: {
			// 获取开关的配置信息
			getSwitch(){
				this.$get(this.$api.getConfigInfo,{},res=>{
					if(res.code === 200){
						this.switchInfo = res.data
						if(this.switchInfo.newbm == '1' && !this.updateFlag){
							uni.$u.toast('当前尚未开放注册新学员，请联系学校电话：0743-8226214');
						}
					}else{
						uni.$u.toast(res.msg ? res.msg : '获取开关信息失败！');
					}
				})
			},
			dialogConfirm(){
				this.show = false
				uni.setStorageSync('dialogIfShowb',false)
			},
			// 提交
			submit() {
				let apiName = !!this.id ? 'editRegister' : 'register'
				if(!!this.id)
				this.model.userInfo.id = this.id
				this.model.userInfo.xh = this.xh
				this.$post(this.$api[apiName], this.model.userInfo, res => {
					if (res.code === 200) {
						uni.$u.toast(`${!!this.id ? '修改':'注册'}成功！个人照片请联系学校工作人员上传`);
						this.model.userInfo = {
							tname: "",
							sex: "",
							dy: '',
							sfz:'',
							cp: '',
							tel: '',
							phone: '',
						};
						setTimeout(()=>{
							uni.redirectTo({
								url:'/pages/index/signUp?xh=' + res.data.xh
							})
						},1000)
					} else if(res.code === 500 && res.msg === '未开放注册'){
						uni.$u.toast("当前尚未开放注册新学员，请联系学校电话：0743-8226214");
					} else if(res.code === 500 && res.msg === '已注册'){
						uni.$u.toast("当前身份证号已经注册！请选择课程");
						setTimeout(()=>{
							uni.redirectTo({
								url:'/pages/index/signUp?sfz=' + this.model.userInfo.sfz
							})
						},1000)
					} else{
						uni.$u.toast(`${res.msg ? res.msg : '网络错误'}`);
					}
				})
			},
			// 重置
			reset() {
				this.$refs.uForm.resetFields();
			},
			sexSelect(e) {
				console.log(e);
				this.model.userInfo.sex = e.name;
				this.$refs.uForm.validateField("userInfo.sex");
			},
			hideKeyboard() {},
			// 出生日期
			birthTime(val) {
				console.log(val);
				this.model.userInfo.bh = this.formatTimestampToYMD(val.value);
				this.showTime = false;
			},
			// 时间戳转化函数
			formatTimestampToYMD(timestamp) {
				// 创建一个 Date 对象，传入时间戳乘以1000转换为毫秒
				const date = new Date(timestamp);

				// 获取年、月、日
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, "0"); // 月份是从0开始的，需要加1
				const day = String(date.getDate()).padStart(2, "0");

				// 拼接成年月日格式的字符串
				const formattedDate = `${year}-${month}-${day}`;

				return formattedDate;
			},
			// 政治面貌
			politics(val) {
				this.model.userInfo.dy = val.value[0].name;
				this.showPolitics = false;
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				console.log(lists, '图片地址');
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			// 上传函数
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://39.108.89.208:2006", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data);
							}, 1000);
						},
					});
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.body {
		padding: 10rpx;
		.title{
			font-size: 34rpx;
			font-weight: 600;
			margin: 20rpx;
		}
		.scrollable-container{
			height: 1000rpx;
			// overflow: auto;
			font-size: 30rpx;
		}
	}
</style>